﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace HHCommon
{
    public class ImageDispose
    {
        #region //给图片添加水印效果方法
        /// <summary>
        /// 定义一个枚举方法确定水印的位置
        /// </summary>
        public enum Location
        {
            LeftTop,        //在图片左上角
            LeftBottom,     //在图片左下角
            RightTop,       //右在图片上角
            RightBottom,    //在图片右下角
            middle          //在图片居中

        }
        /// 图片添加水印效果
        /// </summary>
        /// <param name="ImagePath">原图片的路径</param>
        /// <param name="MarkPath">水印图片的路径</param>
        /// <param name="FileSavepath">把效果图保存在服务器上路径</param>
        /// <param name="locus">把水印设置在图片的位置-枚举类型</param>
        /// <returns>布尔值</returns>
        public bool GetMarkImage(string ImagePath, string MarkPath, string FileSavepath, Location locus)
        {
            bool b=true;
            //获得原图片
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(ImagePath);
            //获得水印图片
            System.Drawing.Image mark = System.Drawing.Image.FromFile(MarkPath);
            int x = 0;
            int y = 0;
            if (originalImage.Height > mark.Height && originalImage.Width > mark.Width)
            {
                switch (locus)
                {
                    case Location.LeftTop:
                        {
                            x = 0;
                            y = 0;
                            break;
                        }
                    case Location.LeftBottom:
                        {
                            x = 0;
                            y = originalImage.Height - mark.Height;
                            break;
                        }
                    case Location.RightTop:
                        {
                            x = originalImage.Width - mark.Width;
                            y = 0;
                            break;
                        }
                    case Location.RightBottom:
                        {
                            x = originalImage.Width - mark.Width;
                            y = originalImage.Height - mark.Height;
                            break;
                        }
                    case Location.middle:
                        {
                            x = originalImage.Height / 2;
                            y = originalImage.Width / 2;
                            break;
                        }
                    default:
                        {
                            x = originalImage.Width - mark.Width;
                            y = originalImage.Height - mark.Height;
                            break;
                        }
                }

                //把原图片新建成一个bmp图片 
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(originalImage);
                //新建一个画板 
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
                //设置高质量插值法 
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度 
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                ////清空画布并以透明背景色填充
                //g.Clear(System.Drawing.Color.Transparent);

                //添加水印
                //string addText = "-百里水上-";
                //System.Drawing.Font f = new Font("楷体_GB2312", 10);
                //Color c = Color.FromArgb(49, 126, 242);
                //System.Drawing.Brush b = new SolidBrush(c);
                //g.DrawString(addText, f, b, 5, toheight - 15);

                //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(mark, new System.Drawing.Rectangle(x, y, mark.Width, mark.Height), 0, 0, mark.Width, mark.Height, GraphicsUnit.Pixel);
                try
                {
                    bitmap.Save(System.Web.HttpContext.Current.Server.MapPath(FileSavepath));
                }
                catch
                {
                  b=false;
                }
                finally
                {
                    originalImage.Dispose();
                    mark.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }
            }
            return b;
        }
        #endregion

        #region //图片生成缩略
        /// <summary>
        /// 生成缩略
        /// </summary>
        /// <param name="fromPath">源图路径（源图在服务器上的路径）</param>
        /// <param name="toPath">保存路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式HW(指定高宽度缩放),W(指定宽缩放),H(指定高缩放),Cut(指定高宽裁剪)</param>  
        public static void saveSmallImg(string fromPath, string toPath, int width, int height, string mode)
        {
            //获得已经保存在服务器上的图片路径
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(fromPath);
            try
            {
                if (originalImage.Height >= height && originalImage.Width >= width)
                {
                    int towidth = width;
                    int toheight = height;

                    int x = 0;
                    int y = 0;
                    int ow = originalImage.Width;
                    int oh = originalImage.Height;
                    #region 方式
                    switch (mode)
                    {
                        case "HW"://指定高宽缩放（可能变形）                
                            break;
                        case "W"://指定宽，高按比例                    
                            toheight = originalImage.Height * width / originalImage.Width;
                            break;
                        case "H"://指定高，宽按比例
                            towidth = originalImage.Width * height / originalImage.Height;
                            break;
                        case "Cut"://指定高宽裁减（不变形）                
                            if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                            {
                                oh = originalImage.Height;
                                ow = originalImage.Height * towidth / toheight;
                                y = 0;
                                x = (originalImage.Width - ow) / 2;
                            }
                            else
                            {
                                ow = originalImage.Width;
                                oh = originalImage.Width * height / towidth;
                                x = 0;
                                y = (originalImage.Height - oh) / 2;
                            }
                            break;
                        default:
                            break;
                    }
                    #endregion

                    //新建一个bmp图片
                    System.Drawing.Bitmap bitmap = new Bitmap(towidth, toheight);

                    //新建一个画板
                    Graphics g = Graphics.FromImage(bitmap);

                    //设置高质量插值法
                    g.InterpolationMode = InterpolationMode.High;

                    //设置高质量,低速度呈现平滑程度
                    g.SmoothingMode = SmoothingMode.HighQuality;

                    //清空画布并以透明背景色填充
                    g.Clear(Color.Transparent);

                    //在指定位置并且按指定大小绘制原图片的指定部分
                    g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);

                    //以jpg格式保存缩略图
                    bitmap.Save(toPath);
                    bitmap.Dispose();
                    g.Dispose();
                }
                else
                    originalImage.Save(toPath);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
            }
        }
        #endregion
    }
}
